from django.db import models
from django.contrib.auth.models import User
from osmp.host.models import Product, Host


# Create your models here.

class Monitor(models.Model):
    mname = models.CharField(max_length=50)
    url = models.CharField(max_length=255)
    tpl = models.CharField(max_length=20)
    freq = models.IntegerField()
    alert_count = models.IntegerField()
    open = models.CharField(max_length=1, choices= (('1','Yes'), ('0', 'No')))
    status = models.CharField(max_length=1, choices= (('1','True'), ('0', 'False')), blank=True)
    table = models.CharField(max_length=100, blank=True)
    pid = models.ForeignKey(Product)
    hid = models.ForeignKey(Host)

    def __unicode__(self):
        return u"%s[%s]" % (self.mname, self.tpl)

    def __str__(self):
        return "%s[%s]" % (self.mname, self.tpl)


class Alert(models.Model):
    mid = models.ForeignKey(Monitor)
    type =  models.CharField(max_length=1)
    timestamp = models.DateTimeField()
    downtime = models.IntegerField(blank=True)
    rtype = models.CharField(max_length=10, blank=True)
    reason = models.TextField(blank=True)
    uid = models.CharField(max_length=20, blank=True)


    def __unicode__(self):
        return u"mid:%s" % (self.mid)

    def __str__(self):
        return "mid:%s" % (self.mid)


class AlertReceive(models.Model):
    mid = models.ForeignKey(Monitor)
    uid = models.ForeignKey(User)
    mode = models.CharField(max_length=4)

    def __unicode__(self):
        return u"%s-%s" % (self.uid, self.mid)

    def __str__(self):
        return "%s-%s" % (self.uid, self.mid)
